/**
 * ⚠️ 重要提示：
 *
 * 本文件包含敏感配置信息，已被添加到 .gitignore 中，不会提交到 Git 仓库。
 *
 * 在部署或开发时，请：
 * 1. 阅读 CONFIG.md 配置指南
 * 2. 创建对应的 .env 文件（如 .env.production、.env.development）
 * 3. 设置正确的环境变量
 *
 * 请勿在此文件中硬编码任何生产环境的敏感信息！
 */

import dotenv from "dotenv";

// 加载环境变量
dotenv.config();

// 环境配置接口
interface Config {
  port: number;
  nodeEnv: string;
  db: {
    host: string;
    port: number;
    name: string;
    user: string;
    password: string;
  };
  jwt: {
    secret: string;
    expiresIn: string;
  };
  cors: {
    origin: string;
  };
  bcrypt: {
    rounds: number;
  };
  oss: {
    accessKeyId: string;
    accessKeySecret: string;
    bucket: string;
    region: string;
    endpoint?: string;
    customDomain?: string;
  };
  deepseek: {
    apiKey: string;
    base_url: string;
    model: string;
  };
}

// 默认配置
const defaultConfig: Config = {
  port: 9999,
  nodeEnv: "development",
  db: {
    host: "47.94.202.171",
    port: 3306,
    name: "cig",
    user: "root",
    password: "G547M!tv#APV77PBjz=~",
  },
  jwt: {
    secret: "xlcig_装机指南_jwt_secret_key_2024",
    expiresIn: "7d",
  },
  cors: {
    origin: "http://192.168.11.193:9999",
  },
  bcrypt: {
    rounds: 12,
  },
  oss: {
    accessKeyId: "LTAI5tP4EhrWG2BADv5yWgtq",
    accessKeySecret: "ujiUEg4RCATJ3ZQNa1s7IdNrQBYaxI",
    bucket: "xlcig",
    region: "oss-cn-beijing",
    endpoint: "https://oss-cn-beijing.aliyuncs.com",
    customDomain: undefined,
  },
  deepseek: {
    apiKey: "sk-a3d9f25299554ffaa2e00a8a2b2e52a6",
    base_url: "https://api.deepseek.com",
    model: "deepseek-chat",
  },
};

// 从环境变量读取配置
export const config: Config = {
  port: parseInt(process.env.PORT || defaultConfig.port.toString()),
  nodeEnv: process.env.NODE_ENV || defaultConfig.nodeEnv,
  db: {
    host: process.env.DB_HOST || defaultConfig.db.host,
    port: parseInt(process.env.DB_PORT || defaultConfig.db.port.toString()),
    name: process.env.DB_NAME || defaultConfig.db.name,
    user: process.env.DB_USER || defaultConfig.db.user,
    password: process.env.DB_PASSWORD || defaultConfig.db.password,
  },
  jwt: {
    secret: process.env.JWT_SECRET || defaultConfig.jwt.secret,
    expiresIn: process.env.JWT_EXPIRES_IN || defaultConfig.jwt.expiresIn,
  },
  cors: {
    origin: process.env.CORS_ORIGIN || defaultConfig.cors.origin,
  },
  bcrypt: {
    rounds: parseInt(
      process.env.BCRYPT_ROUNDS || defaultConfig.bcrypt.rounds.toString()
    ),
  },
  oss: {
    accessKeyId: process.env.OSS_ACCESS_KEY_ID || defaultConfig.oss.accessKeyId,
    accessKeySecret:
      process.env.OSS_ACCESS_KEY_SECRET || defaultConfig.oss.accessKeySecret,
    bucket: process.env.OSS_BUCKET || defaultConfig.oss.bucket,
    region: process.env.OSS_REGION || defaultConfig.oss.region,
    endpoint: process.env.OSS_ENDPOINT || defaultConfig.oss.endpoint,
    customDomain:
      process.env.OSS_CUSTOM_DOMAIN || defaultConfig.oss.customDomain,
  },
  deepseek: {
    apiKey: process.env.DEEPSEEK_API_KEY || defaultConfig.deepseek.apiKey,
    base_url: process.env.DEEPSEEK_BASE_URL || defaultConfig.deepseek.base_url,
    model: process.env.DEEPSEEK_MODEL || defaultConfig.deepseek.model,
  },
};

// 验证必要的环境变量
export const validateConfig = (): void => {
  const requiredEnvVars = [
    "DB_HOST",
    "DB_NAME",
    "DB_USER",
    "DB_PASSWORD",
    "JWT_SECRET",
    "OSS_ACCESS_KEY_ID",
    "OSS_ACCESS_KEY_SECRET",
    "OSS_BUCKET",
    "OSS_REGION",
    "DEEPSEEK_API_KEY",
    "DEEPSEEK_BASE_URL",
    "DEEPSEEK_MODEL",
  ];

  const missingEnvVars = requiredEnvVars.filter(
    (envVar) => !process.env[envVar]
  );

  if (missingEnvVars.length > 0) {
    console.warn(`⚠️ 缺少环境变量: ${missingEnvVars.join(", ")}`);
    console.warn("使用默认配置，生产环境请设置正确的环境变量");
  }
};
